<?php

class InputBuilder {

    public $fieldsArray = array();
    public $db;
    public $sql;
    public $tableName;
    public $pkRecord;

    function build($tableName, $pkRecord = NULL, $tab = NULL) {
        $this->tableName = $tableName;

        $fill = ($pkRecord == NULL ? false : true);
        $rs = array();
        $order = "ORD,FK_TAB";
        $where = sprintf("TABLE_NAME='%s'", $tableName);
        $where.=" AND STATUS=1";
        if ($tab != NULL) {
            $where.=sprintf(" AND FK_TAB=%s ", $tab);
        }
        $rs = $this->db->query_select("cllg_tablefield", $where, $order);
        //echo $this->db->lastQuery;
        foreach ($rs as $row) {
            $row['PK_RECORD'] = $pkRecord;
            switch ($row['CONTROL_TYPE']) {
                case 'input':
                    $this->buildFormField($row['CAPTION'], $this->buildInput($row, $fill));
                    break;
                case 'checkbox':
                    $this->buildFormField($row['CAPTION'], $this->buildCheck($row, $fill));
                    break;
                case 'combo':
                    $this->buildFormField($row['CAPTION'], $this->buildComboBox($row, $fill));
                    break;
                case 'txarea':
                    $this->buildFormField($row['CAPTION'], $this->buildTxArea($row, $fill));
                    break;
                case 'sep':
                    $this->buildSeparator();
                    break;
            }
            $this->fieldsArray[] = $row['NAME'];
        }
    }

    function buildInput($row, $filled = false) {
        $result = '';
        if ($filled) {
            $result = $this->getFieldValue($this->db, $this->tableName, $row['PK_RECORD'], $row['NAME']);
            if (count($result) > 0) {
                $result = $result[0];
                $result = $result['VALUE'];
            } else {
                $result = '';
            }
        }
        $input = "<input style='width:300px' type='text' name='{$row['NAME']}' maxlength='{$row['LONG']}' size='{$row['LONG']}' id='{$row['NAME']}' value='{$result}' />";
        return $input;
    }

    function buildCheck($row, $filled = false) {
        $result = $this->getFieldValue($this->db, $this->tableName, $row['PK_RECORD'], $row['NAME']);
        $input = '<input name="' . $row['NAME']
                . '" id="' . $row['NAME'] . '" value="1" type="checkbox" ' .
                (($filled == true && isset($result[0]["VALUE"]) && $result[0]["VALUE"] == "1") ? 'checked="checked" ' : '') . '/>';
        return $input;
    }

    function buildTxArea($row, $filled = false) {
        $result = $this->getFieldValue($this->db, $this->tableName, $row['PK_RECORD'], $row['NAME']);
        //$result = $result[0];
        $result = isset($result[0]) ? $result[0] : '';
        $input = ' <textarea style="width:300px" name="' . $row['NAME'] . '" rows="5" cols="50" id="' . $row['NAME']
                . '" size="" >' . ($filled == true ? (isset($result["VALUE"]) ? $result["VALUE"] : "" ) : "") . '</textarea>';
        return $input;
    }

    function buildComboBox($row, $filled = false) {
        return '<select  style="width:300px" name="' . $row['NAME'] . '" size="1" id="' . $row['NAME'] . '">' .
                '<option value="">vacio</option> ' . '</select>';
    }

    function buildFormField($label, $input) {
        echo '<tr>';
        echo '<td width="20%">' . strtoupper($label) . '</td>';
        echo '<td width="30%">' . $input .'</td>';
        echo '<td width="20%"></td>';
        echo '<td width="30%"></td></tr>';
    }

    function buildSeparator() {
        echo '<tr colspan="2">';
        echo '  <td><hr/></td></tr>';
    }

    function getFieldValue($db, $tableName, $pkRecord, $fieldName) {
        $sql = "SELECT A.NAME,B.VALUE FROM cllg_tablefield A LEFT JOIN
            cllg_table_valuebyfield B ";
        $sql.=sprintf("ON (A.NAME=B.FK_TABLEFIELD AND B.PK_RECORD='%s') ", $pkRecord);
        $sql.=sprintf("WHERE B.FK_TABLEFIELD='%s' ", $fieldName);
        $sql.=sprintf("AND A.TABLE_NAME='%s' ", $tableName);
        $rs = $db->fetch_all_array($db->query($sql));

        return $rs;
    }

}

?>
